package com.edu.marker.task;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.edu.marker.pojo.ExamInfo;
import com.edu.marker.pojo.PaperInfo;
import com.edu.marker.pojo.QuestionInfo;
import com.edu.marker.pojo.SmallQuestionInfo;
import com.edu.marker.pojo.UserInfo;

public class WSParser {
	protected static final int MAX_REDIRECT_COUNT = 5;

	/**
	 * 登陆信息解析
	 * @author hushuaijun
	 * @creaetime 2013-7-2 上午10:54:28
	 * @param resp
	 * @return
	 * @throws JSONException
	 */
	public static UserInfo getUserInfo(String resp) throws JSONException {
		final JSONObject object = new JSONObject(resp);
		final UserInfo info = new UserInfo();
		
		final boolean result = object.getBoolean("result");
		final String userName = object.getString("name");
		if (!result) {
			final String message = object.getString("message");
			info.setErrMessage(message);
		}
		info.setResult(result);
		info.setName(userName);
		
		return info;
	}
	
	/**
	 * 第三个接口，解析题目信息
	 * @author hushuaijun
	 * @creaetime 2013-7-2 上午10:54:53
	 * @param resp
	 * @return
	 * @throws JSONException
	 */
	public static ArrayList<SmallQuestionInfo> getQuestionTitleInfo(String resp) throws JSONException {
		final JSONObject object = new JSONObject(resp);
		boolean results = object.getBoolean("result");
		if(results){
			ArrayList<SmallQuestionInfo> list = new ArrayList<SmallQuestionInfo>();
			JSONArray arr = object.optJSONArray("smallQuestion");
			final int len = arr.length();
			JSONObject obj = null;
			//解析需要处理！
			for (int i = 0; i < len; i++) {
				SmallQuestionInfo smallQuestion = new SmallQuestionInfo();
				obj = (JSONObject)arr.opt(i);
				smallQuestion.smallQuestionID =  obj.getString("smallQuestionID").trim();
				smallQuestion.rect =  obj.getString("rect").trim();
				smallQuestion.standard =  obj.getString("sorce").trim();
				list.add(smallQuestion);
			}
			return list;
		}
		return null;
	}
	
	/**
	 * 第四个接口，解析题目图片
	 * @author hushuaijun
	 * @creaetime 2013-7-2 上午10:55:17
	 * @param resp
	 * @return
	 * @throws JSONException
	 */
	public static ArrayList<String> getQuestionPicInfo(String resp) throws JSONException {
		final JSONObject object = new JSONObject(resp);
		boolean results = object.getBoolean("result");
		if(results){
			JSONArray arr = object.optJSONArray("ExamQuestion");
			//解析需要处理！
			final int len = arr.length();
			ArrayList<String> list = new ArrayList<String>();
			for (int i = 0; i < len; i++) {
				list.add(arr.getString(i));
			}
			return list;
		}
		return null;
	}
	
	/**
	 * 第5个接口，解析二进制流，获取图片信息
	 * @author hushuaijun
	 * @creaetime 2013-7-2 上午10:56:02
	 * @param in
	 * @return
	 */
	public static Bitmap getDecodeBitmap(InputStream in) {
		if(in !=null) {
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inPurgeable = true;
			options.inInputShareable = true;
			return BitmapFactory.decodeStream(new FlushedInputStream(in), 
					null, options);
		} 
		
		return null;
	}
	
	static class FlushedInputStream extends FilterInputStream {
		public FlushedInputStream(InputStream inputStream) {
			super(inputStream);
		}

		public long skip(long n) throws IOException {
			long totalBytesSkipped = 0L;
			while (totalBytesSkipped < n) {
				long bytesSkipped = this.in.skip(n - totalBytesSkipped);
				if (bytesSkipped == 0L) {
					int b = read();
					if (b < 0) {
						break;
					}
					bytesSkipped = 1L;
				}

				totalBytesSkipped += bytesSkipped;
			}
			return totalBytesSkipped;
		}
	}
	
	/**
	 * 第2个接口，解析考试信息
	 * @author hushuaijun
	 * @creaetime 2013-7-2 上午10:55:39
	 * @param resp
	 * @return
	 * @throws JSONException
	 */
	public static PaperInfo getPaperInfo(String resp) throws JSONException {
		final JSONObject object = new JSONObject(resp);
		boolean result = object.getBoolean("result");
		
		if (result) {
			PaperInfo paperInfo = new PaperInfo();
			JSONArray arr = object.optJSONArray("exam");
			final int len = arr.length();
			JSONObject obj = null;
			for (int i = 0; i < len; i++) {
				ExamInfo examInfo = new ExamInfo();
				obj = (JSONObject)arr.opt(i);
				examInfo.examID = obj.getString("examID").trim();
				examInfo.examName = obj.getString("examName").trim();
				
				JSONArray arrQ = obj.optJSONArray("question");
				final int lengthQ = arrQ.length();
				JSONObject objQ = null;
				examInfo.question = new ArrayList<QuestionInfo>();
				for(int j = 0; j < lengthQ; j++)
				{
					QuestionInfo questionInfo = new QuestionInfo();
					objQ = (JSONObject)arrQ.opt(i);
					questionInfo.questionID = objQ.getString("questionID").trim();
					questionInfo.questionName = objQ.getString("questionName").trim();
					questionInfo.questionCount = objQ.getString("questionCount").trim();
					String cout = objQ.getString("questionIsCheckedCount").trim();
					if("".equals(cout)||cout==null){
						cout = "0";
					}
					questionInfo.questionIsCheckedCount = cout;
					examInfo.question.add(questionInfo);
				}
				paperInfo.addExamInfo(examInfo);
			}
			return paperInfo;
		}
		
		return null;
	}
}
